package com.minelx.sqlstructures.statements.clause;

import java.io.Serializable;
import java.util.Objects;

// FIXME 2021/7/14  wait for me!!!     remove Serializable
public class Field implements Sql, Serializable {
	private static final long serialVersionUID = 2747417460375738957L;

	private final String name;

	private final String as;

	private Field(String name, String as) {
		this.name = name;
		this.as = as;
	}

	public String representName() {
		if (as == null) {
			return name;
		}
		return as;
	}

	@Override
	public String toText() {
		if (as == null) {
			return name;
		}
		return name + " AS " + "\"" + as + "\"";
	}

	public String getName() {
		return name;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		Field field1 = (Field) o;
		return representName().equals(field1.representName());
	}

	@Override
	public int hashCode() {
		return Objects.hash(name, as);
	}

	public static Field of(String name) {
		return as(name, null);
	}

	public static Field as(String name, String as) {
		return new Field(name, as);
	}
}
